package andev.axplorer.Tiles;

import android.content.Context;
import android.graphics.Color;
import android.os.Environment;
import android.text.format.Formatter;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.File;

import andev.axplorer.MainActivity;
import andev.axplorer.Menu.RightMenu;
import andev.axplorer.R;

public class TilesLayoutStorage extends LinearLayout {
    public static TilesLayoutStorage checkedStorage;
    public static MainActivity mainActivity;
    private GestureDetector gestureDetector;
    private String storageName;

    public TilesLayoutStorage(Context context) {
        super(context);
        mainActivity = (MainActivity) context;
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.tiles_layout_storage, this, true);

        gestureDetector = new GestureDetector(context,
                new GestureDetector.SimpleOnGestureListener() {
                    @Override
                    public boolean onDoubleTap(MotionEvent e) {
                        goInDir();
                        return super.onDoubleTap(e);
                    }

                    @Override
                    public boolean onSingleTapUp(MotionEvent e) {
                        setChecked();
                        return super.onSingleTapUp(e);
                    }

                    @Override
                    public boolean onDown(MotionEvent e) {
                        mainActivity.hideKeyboard(true);
                        RightMenu.cancelRightMenu();
                        return true;
                    }


                    @Override
                    public void onLongPress(MotionEvent e) {
                        RightMenu.showRightMenu(TilesLayoutStorage.this, (int) e.getX(), (int) e.getY());
                        super.onLongPress(e);
                    }
                });
    }

    public static void showInternDirectories() {
        File[] files = new File[]{Environment.getDataDirectory(), Environment.getDownloadCacheDirectory(), Environment.getRootDirectory()};
        mainActivity.showFiles(files);
    }

    public static void showExternDirectories() {
        File[] files = Environment.getExternalStorageDirectory().listFiles();
        mainActivity.showFiles(files);
    }

    protected void setChecked() {
        if (checkedStorage != null) {
            checkedStorage.setBackgroundColor(Color.TRANSPARENT);
        }
        TilesLayoutStorage.checkedStorage = this;
        setBackgroundColor(Color.parseColor("#500000ff"));
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            performClick();
        }
        return gestureDetector.onTouchEvent(event);
    }

    public void goInDir() {
        switch (storageName) {
            case "手机存储":
                MainActivity.ResetFilePathHistory();
                MainActivity.addPathNow("手机存储");

                showInternDirectories();
                break;
            default:
                MainActivity.ResetFilePathHistory();
                MainActivity.addPathNow(Environment.getExternalStorageDirectory().toString());

                showExternDirectories();
                break;
        }
    }

    public void showStorageSize(Context context, int order) {
        TextView tv_storage_size = (TextView) findViewById(R.id.tv_storage_size);
        ProgressBar pb_storage_status = (ProgressBar) findViewById(R.id.pb_storage_status);
        long lfree, ltotal;
        switch (order) {
            case 0:
                lfree = Environment.getDataDirectory().getFreeSpace() + Environment.getDownloadCacheDirectory().getFreeSpace() + Environment.getRootDirectory().getFreeSpace();
                ltotal = Environment.getDataDirectory().getTotalSpace() + Environment.getDownloadCacheDirectory().getTotalSpace() + Environment.getRootDirectory().getTotalSpace();

                pb_storage_status.setProgress((int) (100 - (float) lfree / ltotal * 100));
                break;
            default:
                lfree = Environment.getExternalStorageDirectory().getFreeSpace();
                ltotal = Environment.getExternalStorageDirectory().getTotalSpace();

                pb_storage_status.setProgress((int) (100 - (float) lfree / ltotal * 100));
                break;
        }

        String free = Formatter.formatFileSize(context, lfree), total = Formatter.formatFileSize(context, ltotal);
        tv_storage_size.setText(free + " of " + total);
    }

    public void setStorageName(String name) {
        storageName = name;
        TextView tv_storage_name = (TextView) findViewById(R.id.tv_storage_name);
        tv_storage_name.setText(name);
    }

}
